package edu.hubu.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import net.moon.jpa.model.BaseEntity;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @author moonlan
 * @date 2020/10/18 下午12:03
 */
@Entity
@Table(name = "institution")
//逻辑删除注解，删除sql变成了update
@SQLDelete(sql = "update institution set institution_is_deleted = true where institution_id = ?")
//where条件带上了逻辑删除条件
@Where(clause = "institution_is_deleted = false")
@ApiModel("学院")
@JsonIgnoreProperties(value = {"hibernateLazyInitializer", "handler", "fieldHandler"})
public class Institution extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "snowflakeId")
    @GenericGenerator(name = "snowflakeId", strategy = "edu.hubu.commons.SnowflakeId", parameters = {@org.hibernate.annotations.Parameter(name = "prefix", value = "INSTITUTION")})
    @Column(name = "institution_id")
    @ApiModelProperty(value = "institution的id", position = 1)
    private String institutionId;

    @Column(name = "institution_name")
    @ApiModelProperty(value = "学院的名称", position = 2)
    private String institutionName;

    @Column(name = "institution_is_forbidden")
    @ApiModelProperty(value = "学院是否被禁用", position = 3)
    private boolean institutionIsForbidden = false;

    @Column(name = "institution_is_deleted")
    @ApiModelProperty(value = "学院是否被删除", position = 4)
    private boolean institutionIsDeleted = false;

    @OneToMany(mappedBy = "institution", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Major> majors = new ArrayList<>();

    @OneToMany(mappedBy = "institution", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Student> students = new ArrayList<>();

    @OneToMany(mappedBy = "institution", fetch = FetchType.LAZY)
    @ApiModelProperty(hidden = true)
    @JsonIgnore
    private List<Teacher> teachers = new ArrayList<>();

    public Institution(String institutionId, String institutionName, boolean institutionIsForbidden, boolean institutionIsDeleted, List<Major> majors, List<Student> students, List<Teacher> teachers) {
        this.institutionId = institutionId;
        this.institutionName = institutionName;
        this.institutionIsForbidden = institutionIsForbidden;
        this.institutionIsDeleted = institutionIsDeleted;
        this.majors = majors;
        this.students = students;
        this.teachers = teachers;
    }

    public Institution() {
    }

    public static InstitutionBuilder builder() {
        return new InstitutionBuilder();
    }

    public String getInstitutionId() {
        return this.institutionId;
    }

    public String getInstitutionName() {
        return this.institutionName;
    }

    public boolean isInstitutionIsForbidden() {
        return this.institutionIsForbidden;
    }

    public boolean isInstitutionIsDeleted() {
        return this.institutionIsDeleted;
    }

    public List<Major> getMajors() {
        return this.majors;
    }

    public List<Student> getStudents() {
        return this.students;
    }

    public List<Teacher> getTeachers() {
        return this.teachers;
    }

    public void setInstitutionId(String institutionId) {
        this.institutionId = institutionId;
    }

    public void setInstitutionName(String institutionName) {
        this.institutionName = institutionName;
    }

    public void setInstitutionIsForbidden(boolean institutionIsForbidden) {
        this.institutionIsForbidden = institutionIsForbidden;
    }

    public void setInstitutionIsDeleted(boolean institutionIsDeleted) {
        this.institutionIsDeleted = institutionIsDeleted;
    }

    public void setMajors(List<Major> majors) {
        this.majors = majors;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

    public void setTeachers(List<Teacher> teachers) {
        this.teachers = teachers;
    }

    public static class InstitutionBuilder {
        private String institutionId;
        private String institutionName;
        private boolean institutionIsForbidden;
        private boolean institutionIsDeleted;
        private List<Major> majors;
        private List<Student> students;
        private List<Teacher> teachers;

        InstitutionBuilder() {
        }

        public InstitutionBuilder institutionId(String institutionId) {
            this.institutionId = institutionId;
            return this;
        }

        public InstitutionBuilder institutionName(String institutionName) {
            this.institutionName = institutionName;
            return this;
        }

        public InstitutionBuilder institutionIsForbidden(boolean institutionIsForbidden) {
            this.institutionIsForbidden = institutionIsForbidden;
            return this;
        }

        public InstitutionBuilder institutionIsDeleted(boolean institutionIsDeleted) {
            this.institutionIsDeleted = institutionIsDeleted;
            return this;
        }

        public InstitutionBuilder majors(List<Major> majors) {
            this.majors = majors;
            return this;
        }

        public InstitutionBuilder students(List<Student> students) {
            this.students = students;
            return this;
        }

        public InstitutionBuilder teachers(List<Teacher> teachers) {
            this.teachers = teachers;
            return this;
        }

        public Institution build() {
            return new Institution(institutionId, institutionName, institutionIsForbidden, institutionIsDeleted, majors, students, teachers);
        }

        public String toString() {
            return "Institution.InstitutionBuilder(institutionId=" + this.institutionId + ", institutionName=" + this.institutionName + ", institutionIsForbidden=" + this.institutionIsForbidden + ", institutionIsDeleted=" + this.institutionIsDeleted + ", majors=" + this.majors + ", students=" + this.students + ", teachers=" + this.teachers + ")";
        }
    }
}
